<?php
require_once("../util/SessionManager.php");
require_once("../util/SimpleMap.php");
require_once("../util/XConstants.php");

function process() {
   $op = isset($_POST["op"]) ? $_POST["op"] : "";

   if($op == XConstants::$OP_LOGIN) {
      processLogin($_POST);
   }
   else if($op == XConstants::$OP_CHECK_LOGIN) {
      processCheckLogin();
   }
   else if($op == XConstants::$OP_GET_SNACKS) {
      processGetSnacks($_POST);
   }
   else if($op == XConstants::$OP_UPDATE_SNACK) {
      processUpdateSnacks($_POST);
   }
   else if($op == XConstants::$OP_ADD_NEWS) {
      processAddNews($_POST);
   }
   else {
      print_r($_POST);
   }
}

function processLogin($DATA) {
   require_once("../es/Manager.php");

   $name = $DATA[XConstants::$FORM_USER_NAME];
   $pwd = $DATA[XConstants::$FORM_USER_PWD];
   $manager = new Manager();
   $manager->setID($name);
   $manager->setPassword($pwd);
   $res = $manager->login();
   $rsps = new SimpleMap();
   $rsps->put(XConstants::$RESPONSE_CODE, $res);
   echo $rsps->writeBody();
}

function processCheckLogin() {
   $rsps = new SimpleMap();
   $session = new SessionManager();
   $user = $session->getUser();

   if($user != null) {
      $rsps->put(XConstants::$RESPONSE_CODE, XConstants::$SUCCESS);
   }
   else {
      $rsps->put(XConstants::$RESPONSE_CODE, XConstants::$ERROR);
   }

   echo $rsps->writeBody();
}

function processGetSnacks($DATA) {
   require_once("../es/Snacks.php");

   $snacks = new Snacks();
   $snacks->queryInfo();
   $rsps = new SimpleMap();
   $rsps->put(XConstants::$RESPONSE_CODE, XConstants::$SUCCESS);
   $values = $snacks->getSnacksMap()->values();

   for($i = 0; $i < count($values); $i++) {
      $rsps->put("snack" . $i, $values[$i]->toString());
   }

   echo $rsps->writeBody();
}

function processUpdateSnacks($DATA) {
   require_once("../es/Snack_W.php");

   $opType = $DATA[XConstants::$FORM_SNACK_OP];
   $snack = new Snack_W();

   if($opType != XConstants::$TYPE_OP_ADD) {
      $snack->setID($DATA[XConstants::$FORM_SNACK_ID]);
   }

   if($opType != XConstants::$TYPE_OP_DELETE) {
      $snack->setName($DATA[XConstants::$FORM_SNACK_NAME]);
      $snack->setPrice($DATA[XConstants::$FORM_SNACK_PRICE]);
      $snack->setType($DATA[XConstants::$FORM_SNACK_TYPE]);
      $snack->setDescription($DATA[XConstants::$FORM_SNACK_DESC]);
   }

   $res = XConstants::$UNKNOWN;

   if($opType == XConstants::$TYPE_OP_UPDATE) {
      $res = $snack->updateDB();
   }
   else if($opType == XConstants::$TYPE_OP_ADD) {
      $res = $snack->insertDB();
   }
   else if($opType == XConstants::$TYPE_OP_DELETE) {
      $res = $snack->removeDB();
   }

   $rsps = new SimpleMap();
   $rsps->put(XConstants::$RESPONSE_CODE, $res);
   echo $rsps->writeBody();
}

function processAddNews($DATA) {
   require_once("../es/News_W.php");

   $news = new News_W();
   $news->setTitle($DATA[XConstants::$FORM_NEWS_TITLE]);
   $news->setContent($DATA[XConstants::$FORM_NEWS_CONTENT]);
   $res = $news->insertDB();

   $rsps = new SimpleMap();
   $rsps->put(XConstants::$RESPONSE_CODE, $res);
   echo $rsps->writeBody();
}

function encode($str) {
   return $str;
}

process();
?>